//
//  WeatherView.m
//  SwiftHealing
//
//  Created by gd on 16/8/22.
//  Copyright © 2016年 ios0610. All rights reserved.
//

#import "FBShimmering.h"
#import "FBShimmeringView.h"
#import "NSObject+YYModel.h"
#import "UIImageView+WebCache.h"
#import "WeatherModel.h"
#import "WeatherView.h"
#import "YYModel.h"

@interface WeatherView ()
@property(weak, nonatomic) IBOutlet UILabel *cityLabel;
@property(weak, nonatomic) IBOutlet UILabel *weatherLabel;
@property(weak, nonatomic) IBOutlet UILabel *temLabel;
@property(weak, nonatomic) IBOutlet UILabel *suggestLabel;
@property(weak, nonatomic) IBOutlet UILabel *bigtemLabel;
@property(weak, nonatomic) IBOutlet UILabel *windLabel;
@property(weak, nonatomic) IBOutlet UIImageView *weatherImage;
@property(weak, nonatomic) IBOutlet UIImageView *weatherBGV;

@end
@implementation WeatherView

+ (instancetype)loadWeatherView {
  return
      [[[NSBundle mainBundle] loadNibNamed:@"WeatherView" owner:nil options:nil]
          lastObject];
}

- (void)getWeatherDataWithCityName:(NSString *)cityName {

  self.weatherBGV.frame = self.frame;
    NSLog(@"%@",NSStringFromCGRect(self.frame));

//  NSString *urlString =
//      [NSString stringWithFormat:@"http://v.juhe.cn/weather/"
//                                 @"index?format=2&cityname=%@&key="
//                                 @"5e9ef668888c03a78f510fe6aace5070",
//                                 cityName];
    
    NSString *urlString = [NSString stringWithFormat:@"http://api.map.baidu.com/telematics/v3/weather?location=%@&output=json&ak=17IvzuqmKrK1cGwNL6VQebF9",cityName];
  urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:
                             [NSCharacterSet URLQueryAllowedCharacterSet]];

  NSURL *url = [NSURL URLWithString:urlString];
    NSLog(@"%@",url);
  NSURLRequest *request = [NSURLRequest requestWithURL:url];

  [NSURLConnection
      sendAsynchronousRequest:request
                        queue:[NSOperationQueue mainQueue]
            completionHandler:^(NSURLResponse *_Nullable response,
                                NSData *_Nullable data,
                                NSError *_Nullable connectionError) {
              if (connectionError != nil || data.length == 0) {
                NSLog(@"请求错误! %@", connectionError);
                return;
              }

              NSDictionary *ans = [NSJSONSerialization
                  JSONObjectWithData:data
                             options:NSJSONReadingAllowFragments
                               error:NULL];

//                NSArray *array = [NSArray array];
                NSArray *array = ans[@"results"];
                NSDictionary *dict1 = array.lastObject;
                NSArray *weatherArray = dict1[@"weather_data"];
                NSDictionary *dict2 = weatherArray.firstObject;
                NSArray *indexArray = dict1[@"index"];
                NSDictionary *dict3 = indexArray[3];
                WeatherModel *model1 = [WeatherModel yy_modelWithDictionary:dict1];
                WeatherModel *model2 = [WeatherModel yy_modelWithDictionary:dict2];
                WeatherModel *model3 = [WeatherModel yy_modelWithDictionary:dict3];
                
                self.cityLabel.text = model1.currentCity;
                self.weatherLabel.text = model2.weather;
                self.temLabel.text = model2.temperature;
                self.suggestLabel.text = model3.des;
                self.windLabel.text = [NSString stringWithFormat:@"PM25:%d",model1.pm25];
                self.bigtemLabel.text = [[NSString stringWithFormat:@"%@",model2.date] substringWithRange:NSMakeRange(14, 3)];

              if ([self.weatherLabel.text isEqualToString:@"晴"]) {
                self.weatherImage.image = [UIImage imageNamed:@"qing"];
                NSURL *localURL =
                    [[NSBundle mainBundle] URLForResource:@"万里乌云.gif"
                                            withExtension:nil];
                [self.weatherBGV sd_setImageWithURL:localURL];
              } else if ([self.weatherLabel.text isEqualToString:@"多云"]) {
                self.weatherImage.image = [UIImage imageNamed:@"duoyun"];
                NSURL *localURL =
                    [[NSBundle mainBundle] URLForResource:@"万里乌云.gif"
                                            withExtension:nil];
                [self.weatherBGV sd_setImageWithURL:localURL];
              } else if ([self.weatherLabel.text isEqualToString:@"阴"]) {
                self.weatherImage.image = [UIImage imageNamed:@"yin"];
                NSURL *localURL =
                    [[NSBundle mainBundle] URLForResource:@"万里乌云.gif"
                                            withExtension:nil];
                [self.weatherBGV sd_setImageWithURL:localURL];
              } else if ([self.weatherLabel.text isEqualToString:@"阵雨"]) {
                self.weatherImage.image = [UIImage imageNamed:@"zhenyu"];
                NSURL *localURL =
                    [[NSBundle mainBundle] URLForResource:@"细雨绵绵.jpg"
                                            withExtension:nil];
                [self.weatherBGV sd_setImageWithURL:localURL];
              } else if ([self.weatherLabel.text isEqualToString:@"雷"
                                                                 @"阵雨"]) {
                self.weatherImage.image = [UIImage imageNamed:@"zhenyu"];
                NSURL *localURL =
                    [[NSBundle mainBundle] URLForResource:@"细雨绵绵.jpg"
                                            withExtension:nil];
                [self.weatherBGV sd_setImageWithURL:localURL];
              } else if ([self.weatherLabel.text isEqualToString:@"小雨"]) {
                self.weatherImage.image = [UIImage imageNamed:@"xiaoyu"];
                NSURL *localURL =
                    [[NSBundle mainBundle] URLForResource:@"细雨绵绵.jpg"
                                            withExtension:nil];
                [self.weatherBGV sd_setImageWithURL:localURL];
              } else if ([self.weatherLabel.text isEqualToString:@"中雨"]) {
                self.weatherImage.image = [UIImage imageNamed:@"xiaoyu"];
                NSURL *localURL =
                    [[NSBundle mainBundle] URLForResource:@"细雨绵绵.jpg"
                                            withExtension:nil];
                [self.weatherBGV sd_setImageWithURL:localURL];
              } else if ([self.weatherLabel.text
                             isEqualToString:@"多"
                                             @"云转阴"]) {
                self.weatherImage.image = [UIImage imageNamed:@"duoyun"];
                NSURL *localURL =
                    [[NSBundle mainBundle] URLForResource:@"万里乌云.gif"
                                            withExtension:nil];
                [self.weatherBGV sd_setImageWithURL:localURL];
              } else if ([self.weatherLabel.text
                             isEqualToString:@"晴"
                                             @"转多云"]) {
                self.weatherImage.image = [UIImage imageNamed:@"duoyun"];
                NSURL *localURL =
                    [[NSBundle mainBundle] URLForResource:@"万里乌云.gif"
                                            withExtension:nil];
                [self.weatherBGV sd_setImageWithURL:localURL];
              }

              FBShimmeringView *shimmeringView1 = [[FBShimmeringView alloc]
                  initWithFrame:CGRectMake(8, 21, 60, 24)];
              FBShimmeringView *shimmeringView2 = [[FBShimmeringView alloc]
                  initWithFrame:CGRectMake(8, 50, 60, 17)];
              FBShimmeringView *shimmeringView3 = [[FBShimmeringView alloc]
                  initWithFrame:CGRectMake(8, 69, 66, 17)];
              FBShimmeringView *shimmeringView4 = [[FBShimmeringView alloc]
                  initWithFrame:CGRectMake(8, 94, 362, 41)];
              FBShimmeringView *shimmeringView5 = [[FBShimmeringView alloc]
                  initWithFrame:CGRectMake(227, 11, 120, 42)];
              FBShimmeringView *shimmeringView6 = [[FBShimmeringView alloc]
                  initWithFrame:CGRectMake(252, 61, 88, 20.5)];
              FBShimmeringView *shimmeringView7 = [[FBShimmeringView alloc]
                  initWithFrame:CGRectMake(69, 8, 48, 45)];
//
              [self addSubview:shimmeringView1];
              [self addSubview:shimmeringView2];
              [self addSubview:shimmeringView3];
              [self addSubview:shimmeringView4];
              [self addSubview:shimmeringView5];
              [self addSubview:shimmeringView6];
              [self addSubview:shimmeringView7];

              shimmeringView1.contentView = self.cityLabel;
              shimmeringView1.shimmeringSpeed = 80;
              shimmeringView1.shimmering = YES;
              shimmeringView2.contentView = self.weatherLabel;
              shimmeringView2.shimmeringSpeed = 80;
              shimmeringView2.shimmering = YES;
              shimmeringView3.contentView = self.temLabel;
              shimmeringView3.shimmeringSpeed = 80;
              shimmeringView3.shimmering = YES;
              shimmeringView4.contentView = self.suggestLabel;
              shimmeringView4.shimmeringSpeed = 200;
              shimmeringView4.shimmering = YES;
              shimmeringView5.contentView = self.bigtemLabel;
                shimmeringView5.shimmeringSpeed = 230;//140;
              shimmeringView5.shimmering = YES;
              shimmeringView6.contentView = self.windLabel;
                shimmeringView6.shimmeringSpeed = 230;//80;
              shimmeringView6.shimmering = YES;
              shimmeringView7.contentView = self.weatherImage;
                shimmeringView7.shimmeringSpeed = 230;//60;
              shimmeringView7.shimmering = YES;
            }];
}

@end
